package com.hdu.careerplan.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hdu.careerplan.configuration.FilepathConfiguration;
import com.hdu.careerplan.enums.CommandPatternEnum;
import com.hdu.careerplan.mapper.CompetenceInfoMapper;
import com.hdu.careerplan.mapper.PositionInfoMapper;
import com.hdu.careerplan.mapper.PositionInterestScoreMapper;
import com.hdu.careerplan.pojo.dto.AssessmentScoreDTO;
import com.hdu.careerplan.pojo.entity.CompetenceInfo;
import com.hdu.careerplan.pojo.entity.PositionInfo;
import com.hdu.careerplan.pojo.entity.PositionInterestScore;
import com.hdu.careerplan.service.AssessmentService;
import com.hdu.careerplan.util.PythonProcessUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Service
public class AssessmentServiceImpl implements AssessmentService {

    @Resource
    private PositionInfoMapper positionInfoMapper;
    @Resource
    private PositionInterestScoreMapper positionInterestScoreMapper;

    @Resource
    private CompetenceInfoMapper competenceInfoMapper;

    @Override
    public List<PositionInfo> getPositionList() {
        QueryWrapper<PositionInfo> queryWrapper = new QueryWrapper<>();
        return positionInfoMapper.selectList(queryWrapper);
    }

    @Override
    public int uploadAssessmentInterestScore(List<AssessmentScoreDTO> scoreList, Long userId) {
        //TODO 批量插入的优化
        long timestamp = System.currentTimeMillis();
        for (AssessmentScoreDTO score : scoreList) {
            PositionInterestScore completedScore = new PositionInterestScore();
            completedScore.setUserId(userId);
            completedScore.setPositionId(score.getPositionId());
            completedScore.setScore(score.getScore());
            completedScore.setTimestamp(String.valueOf(timestamp));
            positionInterestScoreMapper.insert(completedScore);
        }

        QueryWrapper<PositionInterestScore> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("timestamp", timestamp).eq("user_id", userId);
        positionInterestScoreMapper.delete(queryWrapper);

        return 0;


    }

    @Override
    public List<CompetenceInfo> getCompetenceScore(Long userId) throws IOException, InterruptedException {
        // 删除历史数据
        QueryWrapper<CompetenceInfo> queryWrapperForDelete = new QueryWrapper<>();
        queryWrapperForDelete.eq("user_id", userId);
        competenceInfoMapper.delete(queryWrapperForDelete);
        // 启动python
        String pythonCommand = PythonProcessUtil.generatePythonCommand(FilepathConfiguration.pyDirPath2, FilepathConfiguration.pyName,
                CommandPatternEnum.COMPETENCE_INFO, String.valueOf(userId));
        PythonProcessUtil.startPythonProcess(pythonCommand);
        // 获取新的数据
        QueryWrapper<CompetenceInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("direction", "match_skills", "score","rec_skills","suggestions").eq("user_id", userId);
        return competenceInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<Integer> getAssessmentInterestScore(Long userId) {
        List<Integer> integers = positionInterestScoreMapper.listScoreByUserId(userId);
        if (integers.size() != 20) return null;// 只要不为20个数据就返回null
        return integers;
    }


}
